package cn.aitrox.ry.service.auth.controller;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.auth.api.AuthUserResourceApi;
import cn.aitrox.ry.service.auth.dto.resource.out.AuthResourceDetailDto;
import cn.aitrox.ry.service.auth.dto.user.resource.out.AuthUserResourceListOutDto;
import cn.aitrox.ry.service.auth.entity.AuthResourceEntity;
import cn.aitrox.ry.service.auth.service.AuthPermissionService;
import cn.aitrox.ry.service.auth.service.AuthUserResourceService;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class AuthUserResourceController implements AuthUserResourceApi {

    @Autowired
    private AuthUserResourceService authUserResourceService;

    @Autowired
    private AuthPermissionService permissionService;

    @PutMapping(value = "/auth/user/resource/list")
    @Override
    public RespResult<AuthUserResourceListOutDto> list(@PathVariable("userId") Integer userId) {
        List<AuthResourceEntity> list = authUserResourceService.list(userId);
        List<AuthResourceDetailDto> resourceDetailDtos = null;
        if (!ArrayUtil.isEmpty(list)) {
            resourceDetailDtos = list.stream().map(x -> permissionService.getAuthResourceDetail(x.getId(), list)).collect(Collectors.toList());
        }
        return RespResult.ok(new AuthUserResourceListOutDto(resourceDetailDtos));
    }
}
